package buaa.paas.controller;

import buaa.paas.kubectl.ApiUtil;
import buaa.paas.kubectl.DeploymentUtil;
import buaa.paas.kubectl.PodUtil;
import buaa.paas.kubectl.ServiceUtil;
import buaa.paas.model.Response;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1Deployment;
import io.kubernetes.client.openapi.models.V1DeploymentList;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@CrossOrigin(origins = {"http://localhost:8080","null"})
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    ApiUtil apiUtil;

    @Autowired
    DeploymentUtil util;

    @Autowired
    PodUtil podUtil;

    @Autowired
    ServiceUtil serviceUtil;

    @PostMapping("/test")
    public List<Map<String, Object>> logPods() throws ApiException {
        V1PodList list = apiUtil.getCoreApi().listPodForAllNamespaces(null, null,null, null, null, null,null, null, null);
        List<Map<String, Object>> result = new LinkedList<>();
        for(V1Pod pod: list.getItems()){
            result.add(PodUtil.briefPod(pod));
        }
        return result;
    }

    @PostMapping("/createNginx")
    public String createNginx(){
        TreeMap<String, String> labels = new TreeMap<>();
        labels.put("run", "nginx");
        TreeMap<Integer, String> ports = new TreeMap<>();
        ports.put(80, "TCP");
        try{
            util.createDeployment(2, "nginx", "nginx-spring","dench", ports, labels);
        }catch(ApiException e){
            System.out.println(e.getCode());
            System.out.println(e.getMessage());
            System.out.println(e.getResponseBody());
        }
        return "createFinished";
    }

    @PostMapping("/lsPods")
    public Response getPods(){
        try{
            List<TreeMap<String, Object>> result = podUtil.getPodsFromNamespace("dench");
            Response res = new Response(result);
            res.setCode(200);
            res.setMessage("查询成功");
            return res;
        }catch(ApiException e){
            return new Response(500, e.getMessage(), null);
        }
    }

    @PostMapping("/lsDeployments")
    public Response getDeployments(){
        try{
            List<TreeMap<String, Object>> result = util.listDeployment("dench");
            return new Response(1001, result);
        }catch(ApiException e){
            return new Response(500, e.getMessage(), null);
        }
    }

    @PostMapping("/describeDeployment")
    public Response describeDeployment(){
        try{
            V1Deployment d = util.findDeployment("dench", "nginx-spring");
            return new Response(200, DeploymentUtil.describeDeployment(d));
        }catch(ApiException e){
            return new Response(500, e.getMessage(), null);
        }
    }

    @PostMapping("/deleteDeployment")
    public Response deleteDeployment(){
        try{
            apiUtil.getApi().deleteNamespacedDeployment("nginx-spring", "dench", null, null, null, null, null,null);
            return new Response(200, null);
        }catch(ApiException e){
            return new Response(500, e.getMessage(), null);
        }
    }

    @PostMapping("/lsServices")
    public Response lsService(){
        try{
            List<Map<String, Object>> result = serviceUtil.listServices("dench");
            return new Response(200, result);
        }catch(ApiException e){
            return new Response(500, e.getMessage(), null);
        }
    }

    @PostMapping("/deleteService")
    public Response deleteService(){
        try{
            serviceUtil.deleteService("default", "mysql-service");
            return new Response(200, null);
        }catch(ApiException e){
            return new Response(500, null);
        }
    }

    @PostMapping("/createService")
    public Response createService(){
        try{
            TreeMap<String,String> labels = new TreeMap<>();
            labels.put("svc", "transfer");
            TreeMap<String, String> selectors = new TreeMap<>();
            selectors.put("run", "nginx");
            TreeMap<Integer, Integer> transfers = new TreeMap<>();
            transfers.put(32180,80);
            serviceUtil.createService("nginx-svc", "dench", labels, selectors, transfers);
            return new Response(200, null);
        }catch(ApiException e){
            System.out.println(e.getResponseBody());
            return new Response(500, e.getMessage(), null);
        }
    }
}
